import { user, sendReponse, sleep } from "./declare";

process.on('message', async (msg: any) => {
    switch (msg.type) {
        case 'normal':
            const tasksRes = await Do(msg.data)
            process.send!(tasksRes)

            // 这里只打印一个 输出
            console.log('已完成:' + msg.data[0].id)

            break;
        case 'exit':
            process.exit(0)

        default:
            break;
    }



})

async function Do(users: user[]) {
    return await Promise.all(users.map(e => sendItem(e)))
}

async function sendItem(item: user): Promise<sendReponse> {
    try {
        /*const rsp = await axios({
            url: "url",
            headers: {
                "Content-Type": "application/json;charset=utf-8"
            },
            data: {
                content: `user:${item.name}`
            }
        })*/
        if (item.id % 100 == 0) {
            throw new Error('模拟发送失败')
        }
        await sleep(600)
        return {
            code: 1,
            data: item.id
        }
    } catch (error: any) {
        return {
            code: 1,
            data: item,
            err: error
        }
    }



}